package com.mejlisse.gwt.ui;

import com.google.gwt.app.place.AbstractRecordEditActivity;
import com.google.gwt.app.place.PlaceController;
import com.google.gwt.app.place.RecordEditView;
import com.google.gwt.requestfactory.shared.Receiver;
import com.google.gwt.requestfactory.shared.RecordRequest;
import com.google.gwt.requestfactory.shared.RequestObject;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.valuestore.shared.SyncResult;
import com.google.gwt.valuestore.shared.Value;
import com.mejlisse.gwt.request.NewsRecord;
import com.mejlisse.gwt.shared.Category;
import com.mejlisse.gwt.request.ApplicationRequestFactory;
import com.mejlisse.gwt.scaffold.generated.NewsEditView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Set;

/**
 * An activity that requests all info on an News, allows the user to edit
 * it, and persists the results.
 */
public class NewsEditActivity extends AbstractRecordEditActivity<NewsRecord> {
	private static RecordEditView<NewsRecord> defaultView;

	private static RecordEditView<NewsRecord> getDefaultView() {
		if (defaultView == null) {
			defaultView = new NewsEditView();
		}
		return defaultView;
	}

	private final ApplicationRequestFactory requests;

	/**
	 * Creates an activity that uses the default singleton view instance.
	 */
	public NewsEditActivity(NewsRecord proxy, ApplicationRequestFactory requests, PlaceController placeController, boolean creating) {
		this(getDefaultView(), proxy, requests, placeController, creating);
	}

	/**
	 * Creates an activity that uses its own view instance.
	 */
	public NewsEditActivity(RecordEditView<NewsRecord> view, NewsRecord proxy, ApplicationRequestFactory requests, PlaceController placeController, boolean creating) {
		super(view, proxy, NewsRecord.class, creating, requests, placeController);
		this.requests = requests;
	}

    @Override
    public void start(Display display, EventBus eventBus) {
    
    getNewsEditView().setCategPickerValues(Arrays.asList(Category.values()));

    
    
        super.start(display, eventBus);
    }


	@Override
	protected RecordRequest<NewsRecord> getFindRequest(Value<Long> id) {
	    return requests.newsRequest().findNews(id);
	}

	protected RequestObject<Void> getPersistRequest(NewsRecord record) {
	    return requests.newsRequest().persist(record);
	}

    private NewsEditView getNewsEditView() {
        return ((NewsEditView) getView());
    }
}
